
package com.kun.auth.handler;

import com.kun.util.WebfluxResponseUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.server.resource.InvalidBearerTokenException;
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 没有传入token
 *
 * @author kun.li
 */
public final class BearerTokenServerAuthenticationEntryPoint implements ServerAuthenticationEntryPoint {


    @Override
    public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException authException) {
        if (authException instanceof InvalidBearerTokenException
                || authException instanceof InsufficientAuthenticationException) {
            return WebfluxResponseUtil.responseFailed(exchange, HttpStatus.FAILED_DEPENDENCY.value(), "token 过期");
        }
        return WebfluxResponseUtil.responseFailed(exchange, 200, HttpStatus.UNAUTHORIZED.value(), authException.getMessage());
    }


}
